package cn.xiaobai.service;

import cn.xiaobai.domain.Regedit;
import cn.xioabai.common.utility.DateUtils;
import cn.xioabai.common.utility.ExecuteUtil;
import cn.xioabai.common.utility.IOUtils;
import org.springframework.stereotype.Service;

import java.io.File;
import java.nio.file.Paths;
import java.util.Objects;

import static java.lang.System.out;

/**
 * 注册表服务
 *
 * @Author yangdaji
 * @Date 2023/4/28 0028
 * @PackageName cn.xiaobai.enums
 * @ClassName RegeditCommandEnum
 */
@Service
public class RegeditCommandService {

    /**
     * 普通注册表
     */
    private static String simpleReg = "/regedit-simple-Template.reg";
    /**
     * 普通注册表路径
     */
    private static String regSimplePath = "HKEY_CLASSES_ROOT\\*\\shell";

    /**
     * 桌面级注册表
     */
    private static String desktopReg = "/regedit-desktop-Template.reg";
    /**
     * 桌面级注册表路径
     */
    private static String regDeskTopPath = "HKEY_CLASSES_ROOT\\*\\shell";


    /**
     * 添加注册表
     *
     * @param regeditSimple
     */
    public void addRegedit(Regedit regeditSimple) {
        // 通过读取文件, 将数据写入文件, 在调用命令进行调用文件进行
        convert(regeditSimple);
        File regeditFile = regeditSimple.getRegeditFile();

        String cmd = "";
        // 读取文件
        String fileToString = IOUtils.readFileToString(regeditFile);
        if (Objects.nonNull(fileToString)) {
            // 替换数据
            String kName = regeditSimple.getKName();
            String kValue = regeditSimple.getKValue();
            String regedit = fileToString.replace("${KEY_NAME}", kName)
                    .replace("${VALUE}", kValue).replace("${TAG}", "");
            String regeditOutput = regeditSimple.getRegeditOutput();
            // 将数据再次写入注册表中
            IOUtils.dataWriteFile(regedit, regeditOutput);
            cmd = "reg import " + regeditOutput;
        }
        // 执行命令
        String executeToString = ExecuteUtil.executeToString(cmd);
        out.println("添加执行结果: " + executeToString);
    }

    /**
     * 删除注册表
     *
     * @param regeditSimple
     */
    public void removeRegedit(Regedit regeditSimple) {
        convert(regeditSimple);
        File regeditFile = regeditSimple.getRegeditFile();

        String cmd = "";
        // 读取文件
        String fileToString = IOUtils.readFileToString(regeditFile);
        if (Objects.nonNull(fileToString)) {
            // 替换数据
            String kName = regeditSimple.getKName();
            String regedit = fileToString.replace("${KEY_NAME}", kName)
                    .replace("${TAG}", "-");
            String regeditOutput = regeditSimple.getRegeditOutput();
            // 将数据再次写入注册表中
            IOUtils.dataWriteFile(regedit, regeditOutput);
            cmd = "reg import " + regeditOutput;
        }
        // 执行命令
        String executeToString = ExecuteUtil.executeToString(cmd);
        out.println("删除执行结果: " + executeToString);

    }

    /**
     * 查询注册表
     *
     * @param regedit
     */
    public void queryRegedit(Regedit regedit) {
        String queryCmd;
        String kName = regedit.getKName();
        Boolean isDeskTopLevel = regedit.getIsDeskTopLevel();
        if (!isDeskTopLevel) {
            if (!Objects.equals(kName,"default")) {
                queryCmd = String.format("reg query %s /s /f %s", regSimplePath, kName);
            } else {
                queryCmd = String.format("reg query %s /s", regSimplePath);
            }
        } else {
            if (!Objects.equals(kName,"default")) {
                queryCmd = String.format("reg query %s /s /f %s", regDeskTopPath, kName);
            } else {
                queryCmd = String.format("reg query %s /s", regDeskTopPath);
            }
        }
        String executeToString = ExecuteUtil.executeToString(queryCmd);
        out.println("查询结果: " + executeToString);
    }


    private void convert(Regedit regedit) {
        // 获取文件名称
        String regeditTemplate = regedit.getRegeditTemplate();
        Boolean isDeskTopLevel = regedit.getIsDeskTopLevel();
        String regeditOutput = regedit.getRegeditOutput();
        if (!isDeskTopLevel) {
            regeditTemplate = regeditTemplate + simpleReg;
        } else {
            regeditTemplate = regeditTemplate + desktopReg;
        }

        String userDir = System.getProperty("user.dir");
        userDir = userDir.replace("\\", "/") + "/";
        String templatePath = userDir + regeditTemplate;
        String templateOutPath = userDir + regeditOutput + "/regedit-" + DateUtils.dateToStringSimple() + ".reg";
        regedit.setRegeditTemplate(templatePath);
        regedit.setRegeditFile(Paths.get(templatePath).toFile());
        regedit.setRegeditOutput(templateOutPath);
    }


}
